package com.syh.app.basic.views.dialog_list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.syh.app.basic.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/4/27 9:32
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class DialogListAdp<T extends DialogListAdp.ShowText> extends RecyclerView.Adapter<DialogListAdp.ViewHolder> {

    private Context mContext;
    List<T> data = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public DialogListAdp(Context context, OnItemClickListener onItemClickListener) {
        this.mContext = context;
        this.mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public DialogListAdp.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dialog_list, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DialogListAdp.ViewHolder holder, final int position) {
        holder.tvItem.setText(data.get(position).getShowText());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onSelect(data.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setNewData(List<T> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItem = itemView.findViewById(R.id.tvItem);
        }
    }

    public interface ShowText {
        String getShowText();
    }

    public interface OnItemClickListener<T extends ShowText> {
        void onSelect(T t);
    }

}
